package com.jinlejia.erp.widget.prospect;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jinlejia.erp.R;

/**
 * 给上传图片时，标题的UI控件
 * @author 王进 on 2018/12/29.
 */

public class ProspectUploadHeaderView extends RelativeLayout {
    public ProspectUploadHeaderView(Context context) {
        this(context,null);
    }

    public ProspectUploadHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    private TextView mLabelNameTextView,mNumTextView,mMustStar,mTipTextView,mNumAfterTextView,mTipText;
    private TextView mNumSupportTextView;
    private int mMaxNumber;
    private boolean mMust;
    private int mNumber;
    private String mLabel;
    private String mTipLabel;
    public ProspectUploadHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false);
        initPaint();
        View view = LayoutInflater.from(context).inflate(R.layout.widget_prospect_header_view, this, true);
        mLabelNameTextView = view.findViewById(R.id.labelName);
        mMustStar = view.findViewById(R.id.roomStar);
        mNumTextView = view.findViewById(R.id.roomNumTextView);
        mTipTextView = view.findViewById(R.id.tipTextView);
        mNumAfterTextView = view.findViewById(R.id.numAfterTextView);
        mNumSupportTextView = view.findViewById(R.id.numSupportTextView);
        mTipText = view.findViewById(R.id.tipText);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ProspectUploadHeaderView);
        int num = ta.getInt(R.styleable.ProspectUploadHeaderView_header_num, 0);
        int maxCount = ta.getInt(R.styleable.ProspectUploadHeaderView_header_maxNum, 0);
        this.mNumber = num;
        this.mMaxNumber = maxCount;
        setNum(num,maxCount);
        String labelName = ta.getString(R.styleable.ProspectUploadHeaderView_header_labelName);
        this.mLabel = labelName;
        setLabelName(labelName);
        boolean isMust = ta.getBoolean(R.styleable.ProspectUploadHeaderView_header_mustField,true);
        boolean enableCount = ta.getBoolean(R.styleable.ProspectUploadHeaderView_enableCount,false);

        this.mTipLabel = ta.getString(R.styleable.ProspectUploadHeaderView_header_tipLabel);
        this.mMust = isMust;
        isMustFields(isMust);
        setEnableCount(enableCount);
        String tipMsg = ta.getString(R.styleable.ProspectUploadHeaderView_header_tipText);
        mTipText.setText(tipMsg);
        ta.recycle();
    }

    private void isMustFields(boolean must){
        if (must){
            mMustStar.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(mTipLabel)){
                mTipTextView.setText("("+mTipLabel);
            }else {
                if (mMaxNumber==0){
                    mTipTextView.setText("(需传");
                    mNumSupportTextView.setText(null);
                }else {
                    mTipTextView.setText("(需传");
                    mNumSupportTextView.setText("，最多"+mMaxNumber+"张");
                }
            }
        }else {
            mMustStar.setVisibility(View.INVISIBLE);
            mTipTextView.setText("(最多");
        }
    }

    private void setEnableCount(boolean enable){
        if (enable){
            mMustStar.setVisibility(View.GONE);
            mTipTextView.setVisibility(View.GONE);
            mNumTextView.setVisibility(View.GONE);
            mNumAfterTextView.setVisibility(View.GONE);
        }
    }

    public boolean isMust(){
        return this.mMust;
    }

    public int getShowNumber(){
        return this.mNumber;
    }

    public String getLabel(){
        return this.mLabel;
    }

    public int getMaxCount(){
        return this.mMaxNumber;
    }

    public void setLabelName(String labelName){
        mLabelNameTextView.setText(labelName);
    }



    public void setNum(int num,int maxCount){
        this.mNumber = num;
        this.mMaxNumber = maxCount;
        mNumTextView.setText(String.valueOf(num));
    }

    private Rect halfRect;
    private void initPaint() {
        mBgPaint = new Paint();
        mBgPaint.setColor(Color.parseColor("#fffbefc2"));
        halfRect = new Rect();
    }

    private Paint mBgPaint;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int measuredHeight = getMeasuredHeight();
        int measuredWidth = getMeasuredWidth();
        halfRect.top = measuredHeight/2;
        halfRect.left = 0;
        halfRect.bottom = measuredHeight;
        halfRect.right = measuredWidth;
        canvas.drawRect(halfRect,mBgPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setMust(boolean must) {
        this.mMust = must;
        isMustFields(must);
    }
}
